package org.adorsys.aderp.orga.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.adorsys.aderp.orga.domain.OrgaAddressRole;
import org.adorsys.aderp.orga.domain.OrgaPerson;
import org.adorsys.aderp.orga.domain.OrgaRelationRole;
import org.adorsys.aderp.orga.domain.PersonAddress;
import org.springframework.data.domain.Page;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/personaddresses")
@Controller
@RooWebScaffold(path = "personaddresses", formBackingObject = PersonAddress.class)
public class PersonAddressController {



    @RequestMapping(params = "form", produces = "text/html")
    public String createForm(@RequestParam(value="orgaPersonId", required=false)Long orgaPersonId,
    		@RequestParam(value="addressRoleId", required=false)Long addressRoleId,
    		Model uiModel) {
    	populateCreateForm(uiModel, new PersonAddress() , orgaPersonId, addressRoleId);
        List<String[]> dependencies = new ArrayList<String[]>();
        if (orgaPersonService.countAllOrgapeople() == 0) {
            dependencies.add(new String[] { "orgaperson", "orgapeople" });
        }
        if (orgaAddressRoleService.countAllOrgaAddressRoles() == 0) {
            dependencies.add(new String[] { "orgaaddressrole", "orgaaddressroles" });
        }
        if (orgaAddressService.countAllOrgaAddresses() == 0) {
            dependencies.add(new String[] { "orgaaddress", "orgaaddresses" });
        }
        uiModel.addAttribute("dependencies", dependencies);
        return "personaddresses/create";
    }

    @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value="orgaPersonId", required=false)Long orgaPersonId,
    		@RequestParam(value="addressRoleId", required=false)Long addressRoleId,
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, Model uiModel) {

    	OrgaPerson orgaPerson = orgaPersonId!=null? orgaPersonService.findOrgaPerson(orgaPersonId):null;
		OrgaAddressRole addressRole = addressRoleId!=null?orgaAddressRoleService.findOrgaAddressRole(addressRoleId):null;

		Page<PersonAddress> personAddressPage = personAddressService.searchPersonAddress(orgaPerson, addressRole, page, size);
		uiModel.addAttribute("personaddresses", personAddressPage.getContent());
		uiModel.addAttribute("maxPages", personAddressPage.getNumberOfElements());
    	

        addDateTimeFormatPatterns(uiModel);
        return "personaddresses/list";
    }

    void populateCreateForm(Model uiModel, PersonAddress personAddress, Long orgaPersonId, Long addressRoleId) {
    	OrgaPerson orgaPerson = orgaPersonId!=null? orgaPersonService.findOrgaPerson(orgaPersonId):null;
		OrgaAddressRole addressRole = addressRoleId!=null? orgaAddressRoleService.findOrgaAddressRole(addressRoleId):null;
		personAddress.setOrgaPerson(orgaPerson);
		personAddress.setAddressRole(addressRole);
		
        uiModel.addAttribute("personAddress", personAddress);
        addDateTimeFormatPatterns(uiModel);
        
        if(orgaPerson!=null){
        	uiModel.addAttribute("orgapeople", Collections.singleton(orgaPerson));        	
        } else {
        	uiModel.addAttribute("orgapeople", orgaPersonService.findAllOrgapeople());        	
        }
        
        if(addressRole!=null){
        	uiModel.addAttribute("orgaaddressroles", Collections.singleton(addressRole));        	
        } else {
            uiModel.addAttribute("orgaaddressroles", orgaAddressRoleService.findAllOrgaAddressRoles());
        }
        uiModel.addAttribute("orgaaddresses", orgaAddressService.findAllOrgaAddresses());
    }
}
